/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import facade.SuiLibFacade;
import facade.exception.NotExistingObjectException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import managedBeans.util.CourseToEdit;
import managedBeans.util.JsfUtil;
import valueObject.CategoryVO;
import valueObject.CourseVO;
import valueObject.ProfessorVO;
import valueObject.UserVO;

/**
 *
 * @author David
 */
@ManagedBean
public class coursesMB implements Serializable{

    private HtmlDataTable table;
    private List<CourseVO> courses;
    private List<CourseVO> coursesByProfessor;
    private String name;
    private String description;
    private CategoryVO categoryVO;
    
    @EJB
    private SuiLibFacade suilibFacade;
    
    private boolean success = false;
    /**
     * Creates a new instance of coursesMB
     */
    public coursesMB() {
    }

    /**
     * @return the courses
     */
    public List<CourseVO> getCourses() {
        
        courses = getSuilibFacade().getAllCoursesVO();
        return courses;
    }

    /**
     * @param courses the courses to set
     */
    public void setCourses(List<CourseVO> courses) {
        this.courses = courses;
    }

    /**
     * @return the coursesByProfessor
     */
    public List<CourseVO> getCoursesByProfessor() {
        System.out.println("Entra a getCoursesByProfessor  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        try {
            String sesionUser = ((UserVO) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user")).getUsername();
            ProfessorVO vo = suilibFacade.findProfessorVOById(sesionUser);
            System.out.println(vo.getUserName());
            //coursesByProfessor = suilibFacade.findCoursesByProfessor(vo);
            //return coursesByProfessor;
            return (List)vo.getCourses();
        } catch (NotExistingObjectException ex) {
            Logger.getLogger(coursesMB.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    
    
    public void createCourse() {
        try {
            CourseVO courseVO = new CourseVO();
            courseVO.setCategory(categoryVO);
            courseVO.setDescription(description);
            courseVO.setName(name);
            String sesionUser = ((UserVO) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user")).getUsername();
            ProfessorVO vo = getSuilibFacade().findProfessorVOById(sesionUser);
            ArrayList<ProfessorVO> listProfessors = new ArrayList<ProfessorVO>();
            listProfessors.add(vo);
            courseVO.setProfessors(listProfessors);
            suilibFacade.createCourse(courseVO);
            success = true;
        } catch (NotExistingObjectException ex) {
            Logger.getLogger(coursesMB.class.getName()).log(Level.SEVERE, null, ex);
            success = false;
        }
        
    }
    
    public SelectItem[] loadCategories() {
        return JsfUtil.getSelectItems(getSuilibFacade().getAllCategoriesVO(), true);
    }

    /**
     * @return the categoryVO
     */
    public CategoryVO getCategoryVO() {
        return categoryVO;
    }

    /**
     * @param categoryVO the categoryVO to set
     */
    public void setCategoryVO(CategoryVO categoryVO) {
        this.categoryVO = categoryVO;
    }

    /**
     * @return the suilibFacade
     */
    public SuiLibFacade getSuilibFacade() {
        return suilibFacade;
    }

    /**
     * @param suilibFacade the suilibFacade to set
     */
    public void setSuilibFacade(SuiLibFacade suilibFacade) {
        this.suilibFacade = suilibFacade;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the descriprion
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param descriprion the descriprion to set
     */
    public void setDescriprion(String description) {
        this.setDescription(description);
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the success
     */
    public boolean isSuccess() {
        return success;
    }
    
    

    /**
     * @return the table
     */
    public HtmlDataTable getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(HtmlDataTable table) {
        this.table = table;
    }
    
    public String editFormList() {
        CourseVO vo =(CourseVO) getTable().getRowData();
        CourseToEdit.getInstance().setId(vo.getId());
        return "editCourse";
    }
}
